package cn.myblog.ainblog.service;

import cn.myblog.ainblog.mapper.ArticleCategoryMapper;
import cn.myblog.ainblog.mapper.ArticleMapper;
import cn.myblog.ainblog.mapper.ArticleTagMapper;
import cn.myblog.ainblog.model.ArticleCategoryModel;
import cn.myblog.ainblog.model.ArticleModel;
import cn.myblog.ainblog.model.ArticleTagModel;
import cn.myblog.ainblog.vo.ArticleAndCategoryVO;
import cn.myblog.ainblog.vo.ArticleAndTagVO;
import cn.myblog.ainblog.vo.ArticleCategoryVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author Dovaior
 * @Description
 * @create 2021-04-13 16:31
 **/
@Service
public class ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleCategoryMapper articleCategoryMapper;
    @Autowired
    private ArticleTagMapper articleTagMapper;

    /**
     * 添加博文
     * @param articleModel 博文的实体类
     * @param categoryName 后端接收到的分类名（实质上接收到的是分类的ID）
     * @param tagNames 后端接收到的标签的List集合，实质上接收到的是标签的ID的集合
     * @Transactional 事务（防止脏读和数据更新）：要么同时完成，要么同时都完不成
     */
    @Transactional
    public void addArticle(ArticleModel articleModel, String categoryName, List<String> tagNames){
        //1.给博文自定义生成UUID
        String articleId = UUID.randomUUID().toString().replaceAll("-", "");
        articleModel.setArticleId(articleId);
        //2.添加博文信息
        Long temp = new Long(0);
        articleModel.setArticleView(temp);
        articleModel.setCommentCount(temp);
        articleModel.setLikeCount(temp);
        articleMapper.insert(articleModel);
        //3.根据传递过来的分类ID，添加到ain_article_category表中
        ArticleCategoryModel model = new ArticleCategoryModel();
        model.setArticleId(articleId);
        model.setCategoryId(categoryName);
        articleCategoryMapper.insert(model);
        //4.根据传递过来的标签名，遍历查询出List中全部tagName的ID，并且添加到ain_article_tags表中
        for (String tagId:tagNames) {
            //创建articleTagModel的对象，因为是多个tagId，因此需要创建多个对象
            ArticleTagModel articleTagModel = new ArticleTagModel();
            //给articleId赋值
            articleTagModel.setArticleId(articleId);
            //给tagId赋值
            articleTagModel.setTagId(tagId);
            //进行ain_article_tags表的插入操作
            articleTagMapper.insert(articleTagModel);
        }
    }

    /**
     * 多表联查 + 分页查询
     * @param page 封装的分页信息
     * @return 返回分页查询完的信息
     */
    public IPage<ArticleCategoryVO> selectArticleAndCategoryListPage(IPage<ArticleCategoryVO> page){
        return articleMapper.selectArticleAndCategoryListPage(page);
    }

    /**
     * 根据ID值删除博文
     * @param articleId 博文ID值
     * @return 返回标志
     */
    @Transactional
    public int deleteArticle(String articleId){
        //1.删除博文分类表中的数据(ain_article_category)
        int categoryFlag = articleCategoryMapper.deleteById(articleId);
        //2.删除博文标签表中的数据(ain_article_tag)
        int tagFlag = articleTagMapper.deleteById(articleId);
        //3.删除博文表中的数据(ain_article)
        int articleFlag = articleMapper.deleteById(articleId);
        //当categoryFlag、tagFlag、articleFlag的返回值都不等于0，说明都删除了数据
        if (categoryFlag!=0 && tagFlag!=0 && articleFlag!=0){
            //删除成功
            return 1;
        }else{
            //删除失败
            return 0;
        }
    }

    /**
     * 根据博文ID查询博文信息
     * @param articleId 博文ID值
     * @return 返回实体类
     * @Transactional 事务
     */
    @Transactional
    public Map<String,Object> queryArticleById(String articleId){
        //给当前博文的浏览数量+1
        articleMapper.viewNumberPlus(articleId);
        //1.根据博文id查询出分类id和分类名
        ArticleAndCategoryVO articleAndCategoryVO = articleCategoryMapper.queryCategoryModelByArticleId(articleId);
        //2.根据博文id查询出标签名
        ArticleAndTagVO articleAndTagVO = articleTagMapper.queryTagListByArticleId(articleId);
        //3.根据博文id查询出博文的全部信息
        ArticleModel articleModel = articleMapper.selectById(articleId);
        //将全部查询出的信息封装到map当中
        Map<String,Object> map = new HashMap();
        map.put("articleModel",articleModel);
        map.put("articleAndCategoryVO",articleAndCategoryVO);
        map.put("articleAndTagVO",articleAndTagVO);
        //返回给Controller层
        return map;
    }

    /**
     * 更新博文
     * @param articleModel 封装的article实体类
     * @param categoryName 分类ID
     * @param tagNames 标签ID的集合
     */
    @Transactional
    public void updateArticle(ArticleModel articleModel, String categoryName, List<String> tagNames){
        //1.更新博文内容
        articleMapper.updateById(articleModel);
        //2.更新分类信息
        ArticleCategoryModel acModel = new ArticleCategoryModel();
        acModel.setArticleId(articleModel.getArticleId());
        acModel.setCategoryId(categoryName);
        articleCategoryMapper.updateById(acModel);
        //3.删除标签关系
        articleTagMapper.deleteById(articleModel.getArticleId());
        //4.添加标签
        for(String tagId : tagNames){
            ArticleTagModel atModel = new ArticleTagModel();
            atModel.setArticleId(articleModel.getArticleId());
            atModel.setTagId(tagId);
            articleTagMapper.insert(atModel);
        }
    }

    /**
     * likeCount前五的博文封面
     * @return 返回查询出来的信息
     */
    public List<ArticleModel> queryArticleCoverLimit5(){
        return articleMapper.queryArticleCoverLimit5();
    }
    /**
     * 查询最新的四条博文，用于在floatBar展示
     * @return 返回查询结果
     */
    public List<ArticleModel> queryArticleModelLimit4(){
        return articleMapper.queryArticleModelLimit4();
    }

    /**
     * 查询三条like count最高的记录，用在前端index页面
     * @return 返回查询结果
     */
    public List<ArticleModel> queryArticleAndContentLimit3(){
        return articleMapper.queryArticleAndContentLimit3();
    }

    /**
     * 查询三条最新创的记录，用在前端index页面
     * @return 返回查询结果
     */
    public List<ArticleModel> queryArticleAndContentLimit3orderdate(){
        return articleMapper.queryArticleAndContentLimit3orderdate();
    }
    /**
     * 查询view最高的8条博文
     * @return 返回查询的结果
     */
    public List<ArticleModel> queryArticleModelByViewLimit8(){
        return articleMapper.queryArticleModelByViewLimit8();
    }

    /**
     * 详情页面：相关推荐的查询
     * @return 返回查询出来的结果集
     */
    public List<ArticleCategoryVO> queryArticleModelByCategoryLimit6(Map<String,String> map){
        return articleMapper.queryArticleModelByCategoryLimit6(map);
    }

    /**
     * 点赞事件（给对应Id的博文点赞）
     * @param articleId 博文Id
     */
    public int likeCountPlusAndQuery(String articleId){
        return articleMapper.likeCountPlusAndQuery(articleId);
    }

    /**
     * 根据博文Id查询点赞数量
     * @param articleId 博文Id
     */
    public Long queryLikeCountById(String articleId){
        return articleMapper.queryLikeCountById(articleId);
    }

    /**
     * 查询指定分类下的全部博文，用于category-subpage的展示
     * @param categoryId 指定的分类ID
     */
    public List<ArticleCategoryVO> queryArticleModelByCategoryToSubPage(String categoryId){
        return articleMapper.queryArticleModelByCategoryToSubPage(categoryId);
    }

    /**
     * @param tagId 指定标签的id，用于在前端标签子页面展示
     */
    public List<ArticleModel> queryArticleListByTag(String tagId){
        return articleMapper.queryArticleListByTag(tagId);
    }

    /**
     * 查询所有博文的id、图片以及分类名，用于photo_page页面
     * (根据图片去重)
     */
    public List<ArticleCategoryVO> queryCoverAndCategoryToPhotoPage(){
        return articleMapper.queryCoverAndCategoryToPhotoPage();
    }

    /**
     * 查询全部博文，在index页面展示
     */
    public List<ArticleModel> selectList(){
        return articleMapper.selectList(null);
    }

    /**
     * 查询全部博文，用于index页面
     */
    public List<ArticleCategoryVO> queryAllArticleToIndex(){
        return articleMapper.queryAllArticleToIndex();
    }

    /**
     * 更新博文留言数量
     * @param articleId 指定的博文id
     */
    public int freshCommentCountByArticleId(String articleId){
        return articleMapper.freshCommentCountByArticleId(articleId);
    }

    /**
     * 删除留言，数量减一
     * @param articleId 指定的博文
     */
    public int deFreshCommentCountByArticleId(String articleId){
        return articleMapper.deFreshCommentCountByArticleId(articleId);
    }

    /**
     * 删除某博文的全部留言，清零留言数量
     * @param articleId 指定的博文id
     */
    public int clearCommentCountByArticleId(String articleId){
        return articleMapper.clearCommentCountByArticleId(articleId);
    }

    /**
     * 根据指定博文id查询博title
     * @param articleId 指定的博文id
     */
    public String queryArticleTitleById(String articleId){
        return articleMapper.queryArticleTitleById(articleId);
    }

    /**
     * 搜索
     * @param keyWord 关键字
     */
    public List<ArticleCategoryVO> search(String keyWord){
        return articleMapper.queryByKeyWord(keyWord);
    }
}
